//
//  JJViewController.m
//  jingdongfang
//
//  Created by 郝志宇 on 16/8/2.
//  Copyright © 2016年 ZhiYu Hao. All rights reserved.
//

#import "JJViewController.h"
#import "IGUsersynopsisApi.h"
@interface JJViewController () <UITableViewDelegate, UITableViewDataSource>
{
    CGFloat contextHeight;
    UILabel *msg;
    NSString *content;
}

@property (weak, nonatomic) IBOutlet UITableView *table;
@property (nonatomic, strong) IGUsersynopsisApi *api;
@end

@implementation JJViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.table.backgroundColor = RGB(234, 234, 234);
    self.api = [IGUsersynopsisApi new];
    [self addHeader];
    [self dataRequest];
}


- (void)dataRequest
{
    self.api.uid = _u_id;
    @KWeakObj(self)
    [self.api startWithDecodeClass:[IGUsersynopsisResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        [selfWeak.table ig_endHeaderRefreshing];
        [selfWeak hidden];
        if (responseObject)
        {
            IGUsersynopsisResp *resp = responseObject;
            IGUsersynopsisModel *model = resp.data;
            if (model.content && model.content.length > 0)
            {
                content = model.content;
            }
            else if (content)
            {
                return;
            }
            else
            {
                [selfWeak show];
            }
            [self.table reloadData];
        }
        else
        {
            if (!content)
            {
                [selfWeak show];
            }
        }
    }];
}

- (void)addHeader
{
    [self.table ig_setHeaderWithRefreshingBlock:^{
        [self dataRequest];
    }];
}

- (void)show
{
    if (!msg)
    {
        msg = [[UILabel alloc] initWithFrame:CGRectZero];
//        msg.text = @"TA透漏着淡淡的文艺气息, 羞涩的一个字也没有留下...";
        msg.text = NSLocalizedString(@"No Self Intro", nil);
        msg.textAlignment = NSTextAlignmentCenter;
        msg.textColor = [UIColor c4Color];
        msg.numberOfLines = 0;
    }
    [self.view addSubview:msg];
    msg.width = self.view.width - 40;
    msg.height = self.view.height;
    msg.center = CGPointMake(self.view.width/2, self.view.height/2);
}

- (void)hidden
{
    [msg removeFromSuperview];
}

#pragma mark - UITableViewDelegate, UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = tableView.backgroundColor;
    
    CGFloat x = cell.separatorInset.left;
    UIFont *font = [UIFont systemFontOfSize:14];
    
    UILabel *context = [[UILabel alloc] initWithFrame:CGRectMake(x, x, KSCREENWIDTH-x*2, 0)];
    context.numberOfLines = 0;
    context.text = content;
    context.textColor = [UIColor c4Color];
    context.font = font;
    [cell.contentView addSubview:context];
    CGSize size = [Tool getLabelSizeWithText:context.text AndWidth:context.width AndFont:font attribute:nil];
    context.height = size.height;

    if (contextHeight != context.height+x+x) {
        contextHeight = context.height+x+x;
        [tableView reloadData];
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return contextHeight;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

@end
